Page({
  data: {
    search: '',
    bookValue: '',
    roomValue: '',
    books: [
      { id: 1, title: '三国演义', nom: '1'},
      { id: 2, title: '水浒传', nom: '2'},
      { id: 3, title: '红楼梦', nom: '3'},
      { id: 4, title: '西游记', nom: '4'},
      { id: 5, title: '苏菲的世界', nom: '5'},
      { id: 6, title: '三国演义义', nom: '6'},
      { id: 7, title: '三国演义义三国演义义', nom: '7'}
    ],
    rooms: [
      { id: 1, title: '2104', nom: '1'},
      { id: 2, title: '2505(软件协会)', nom: '2'},
      { id: 3, title: '2104', nom: '3'},
      { id: 4, title: '2104', nom: '4'},
      { id: 5, title: '2104', nom: '5'},
    ],
    currentBooks: [],
    currentRooms: []
  },
  onLoad(options) {
    this.setData({
      search: options.type,
      currentBooks: this.data.books
    });
  },

  onCellClick(e) {
    const url = e.currentTarget.dataset.url;
    const id = e.currentTarget.dataset.id; 
    wx.navigateTo({
      url: `${url}?id=${id}` 
    });
  },

  onChangeBooks(e) {
    this.setData({
      bookValue: e.detail, 
    });
  },
  onClickBooks() {
    this.filterBooks();
  },
  onSearchBooks() {
    this.filterBooks();
  },
  filterBooks() {
    const { bookValue, books } = this.data;
    const keyword = (bookValue || "").trim().toLowerCase();
    
    if (!keyword) {
      this.setData({ currentBooks: books });
      return;
    }

    const scoredBooks = books.map(book => {
      let score = 0;
      const title = book.title.toLowerCase();
      const nom = String(book.nom).toLowerCase();
      // 简单的 -> 按照关键词在标题的出现频次为最高优先级
      const keywordMatches = (title.match(new RegExp(keyword, 'g')) || []).length;
      score += 2 * keywordMatches; 
      if (title === keyword) score += 5;
      if (nom === keyword) score += 3;
      if (title.includes(keyword)) score += 2;
      else if (nom.includes(keyword)) score += 1;
      return { ...book, score };
    });

    const filteredBooks = scoredBooks
      .filter(item => item.score > 0) 
      .sort((a, b) => {
        if (b.score !== a.score) return b.score - a.score;
        return a.id - b.id;
      });

    this.setData({ currentBooks: filteredBooks });
  },



  onChangeRooms(e) {
    this.setData({
      roomValue: e.detail, 
    });
  },
  onClickRooms() {
    this.filterRooms();
  },
  onSearchRooms() {
    this.filterRooms();
  },
  filterRooms() {
    const { roomValue, rooms } = this.data;
    const keyword = (roomValue || "").trim().toLowerCase();
    
    if (!keyword) {
      this.setData({ currentRooms: rooms });
      return;
    }

    const scoredRooms = rooms.map(room => {
      let score = 0;
      const title = room.title.toLowerCase();
      const nom = String(room.nom).toLowerCase();
      // 简单的 -> 按照关键词在标题的出现频次为最高优先级
      const keywordMatches = (title.match(new RegExp(keyword, 'g')) || []).length;
      score += 2 * keywordMatches; 
      if (title === keyword) score += 5;
      if (nom === keyword) score += 3;
      if (title.includes(keyword)) score += 2;
      else if (nom.includes(keyword)) score += 1;
      return { ...room, score };
    });

    const filteredRooms = scoredRooms
      .filter(item => item.score > 0) 
      .sort((a, b) => {
        if (b.score !== a.score) return b.score - a.score;
        return a.id - b.id;
      });

    this.setData({ currentRooms: filteredRooms });
  }
})